<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;


use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formAppTrait{
    public function index_app(Content $content)
    {
        return $content->title('App相关')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_app()->render()}</div>
HTML
            );
    }
    protected function form_app(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->tab('基本', function (Form $form) {
            $form->text('admin_app_introduce_page', 'App介绍页面地址')->default(Settings::get('admin_app_introduce_page', 'https://www.minisns.cn'));
            $form->text('admin_app_url_schemes', 'urlSchemes')->help('查看<a href="https://img.mini.minisns.cn/images/urlSchemes.jpg" target="_blank">配置说明</a>')->default(Settings::get('admin_app_url_schemes', 'chongyeapp'));

            $form->divider();

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">说明: h5状态下，只有微信浏览器才能登录，所以在普通浏览器时，推荐用户浏览一定的页面数就跳转到下载App页</div>';
            }, '');
            $form->radio('h5_normal_browser_redirect', '非微信浏览器跳转下载页')->options([1=>'开启', 0=>'关闭'])->default(Settings::get('h5_normal_browser_redirect', 0));
            $form->number('h5_normal_browser_redirect_pages', '浏览页面数')->help('默认用户浏览了4页，在第五页时立即跳转')->default(Settings::get('h5_normal_browser_redirect_pages', 5));

            $form->divider();
        });
        $form->tab('版本', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">Tips:<br> 1、是否更新取决于版本号，版本号必须是三个数字，中间两个点，类似 1.0.1<br/>2、最下面新添加的代表当前版本，以前的老版本理论可以删除，仅做备注<br/>3、版本描述的换行使用 \n，不是你电脑的回车<br>4、IOS安装包更新请放跳转store应用商店链接,安卓apk和wgt文件放文件下载链接</div>';
            }, '');
            $form->table('admin_app_android_update','安卓更新', function (NestedForm $table) {
                $table->text('version', '版本号')->placeholder('1.0.1')->required();
                $table->textarea('desc', '版本描述')->placeholder('1、修复了xx\n2、增加了xx')->required();
                $table->select('type', '更新方式')->options(['forcibly'=>'强制更新', 'solicit'=>'弹窗确认更新', 'silent'=>'静默更新'])->default('forcibly')->required();
                $table->text('url', '下载地址')->placeholder('https://www.minisns.cn/chongye.apk')->required();
            })->default(Settings::get('admin_app_android_update', ''));

            $form->table('admin_app_ios_update','IOS更新', function (NestedForm $table) {
                $table->text('version', '版本号')->placeholder('1.0.1')->required();
                $table->textarea('desc', '版本描述')->placeholder('1、修复了xx\n2、增加了xx')->required();
                $table->select('type', '更新方式')->options(['forcibly'=>'强制更新', 'solicit'=>'弹窗确认更新', 'silent'=>'静默更新'])->default('forcibly')->required();
                $table->text('url', '下载地址')->placeholder('https://apps.apple.com/cn/app/%')->required();
            })->default(Settings::get('admin_app_ios_update', ''));
        });

        $form->tab('引导图', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">Tips:<br> 1、必须是三页，如果你不需要引导图，就一个都不要添加<br>2、引导图是首次启用App进行展现的，和启动图区分开，一般介绍你的App主要作用<br>3、点击查看<a href="https://img.mini.minisns.cn/images/guide/example.jpg" target="_blank" style="color: blue;">示例配置</a>，<a href="https://img.mini.minisns.cn/images/guide/guide.gif" target="_blank"  style="color: blue;">示例效果</a></div>';
            }, '');
            $form->table('admin_app_guide','引导图', function (NestedForm $table) {
                $table->text('title', '标题')->default('')->required();
                $table->text('desc', '描述')->default('');
                $table->image('img', '图片')->help('建议和默认图片大小。 <a href="https://img.mini.minisns.cn/images/guide/01_c.png" download="pc_index_right_banner.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default('');
            })->default(Settings::get('admin_app_guide', ''));
        });

        $form->action('settings/action/app');
        return $form;
    }

    public function app(Request $request)
    {
        SiteUtils::forget_config_data();

        $admin_app_android_update = AdminUtils::_table_data($request->input('admin_app_android_update'));
        $admin_app_ios_update = AdminUtils::_table_data($request->input('admin_app_ios_update'));
        $admin_app_guide = AdminUtils::_table_data($request->input('admin_app_guide'));

        Settings::set('admin_app_android_update', $admin_app_android_update, true);
        Settings::set('admin_app_ios_update', $admin_app_ios_update, true);
        Settings::set('admin_app_guide', $admin_app_guide, true);

        Settings::set('admin_app_introduce_page', $request->input('admin_app_introduce_page'), true);
        Settings::set('admin_app_url_schemes', $request->input('admin_app_url_schemes'), true);
        Settings::set('h5_normal_browser_redirect', $request->input('h5_normal_browser_redirect'), true);
        Settings::set('h5_normal_browser_redirect_pages', $request->input('h5_normal_browser_redirect_pages'), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }
}
